<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class barang extends My_Controller
{
	
	function __construct()
	{
		parent::__construct();
		
		$this->load->model('mdl_barang', 'barang');
		
	}
	
	function index()
	{
		$data['can_view'] 	= $this->can_view();
		$data['can_insert'] = $this->can_insert();
		$data['can_update'] = $this->can_update();
		$data['can_delete'] = $this->can_delete();
		
		$this->open();
		
		$config['base_url'] = base_url().'index.php/barang/index/';
		$config['total_rows'] = $this->db->count_all('keberangkatan');
		$config['per_page'] = '50';
		$config['num_links'] = '5';
		$config['uri_segment'] = '3';
		
		$config['full_tag_open'] = '';
		$config['full_tag_close'] = '';
		
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		
		$config['cur_tag_open'] = '<li><a href="javascript:void(0)" class="current">';
		$config['cur_tag_close'] = '</a></li>';
		
		$config['prev_link'] = 'Prev';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		
		$config['next_link'] = 'Next';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
		
		$config['last_link'] = 'Last';
		$config['last_tag_open'] = '<li>';
		$config['last_tag_close'] = '</li>';
		
		$config['first_link'] = 'First';
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		
		$this->pagination->initialize($config);	

		
		$data['results'] = $this->barang->getItem($config['per_page'], $this->uri->segment(3));
		
		$this->load->view('barang/barang_list', $data);
		
		$this->close();
	}
	
	
	function index_view()
	{
		$data['can_view'] 	= $this->can_view();
		$data['can_insert'] = $this->can_insert();
		$data['can_update'] = $this->can_update();
		$data['can_delete'] = $this->can_delete();
		
		$this->open();
		
		$config['base_url'] = base_url().'index.php/barang/index/';
		$config['total_rows'] = $this->db->count_all('keberangkatan');
		$config['per_page'] = '50';
		$config['num_links'] = '5';
		$config['uri_segment'] = '3';
		
		$config['full_tag_open'] = '';
		$config['full_tag_close'] = '';
		
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		
		$config['cur_tag_open'] = '<li><a href="javascript:void(0)" class="current">';
		$config['cur_tag_close'] = '</a></li>';
		
		$config['prev_link'] = 'Prev';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		
		$config['next_link'] = 'Next';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
		
		$config['last_link'] = 'Last';
		$config['last_tag_open'] = '<li>';
		$config['last_tag_close'] = '</li>';
		
		$config['first_link'] = 'First';
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		
		$this->pagination->initialize($config);	

		
		$data['results'] = $this->barang->getItem($config['per_page'], $this->uri->segment(3));
		
		$this->load->view('barang/barang_view', $data);
		
		$this->close();
	}
	
	function refresh_jadwal(){
			
		$config['base_url'] = base_url().'index.php/barang/index/';
		$config['total_rows'] = $this->db->count_all('keberangkatan');
		$config['per_page'] = '50';
		$config['num_links'] = '5';
		$config['uri_segment'] = '3';
		
		$config['full_tag_open'] = '';
		$config['full_tag_close'] = '';
		
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		
		$config['cur_tag_open'] = '<li><a href="javascript:void(0)" class="current">';
		$config['cur_tag_close'] = '</a></li>';
		
		$config['prev_link'] = 'Prev';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		
		$config['next_link'] = 'Next';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
		
		$config['last_link'] = 'Last';
		$config['last_tag_open'] = '<li>';
		$config['last_tag_close'] = '</li>';
		
		$config['first_link'] = 'First';
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		
		$this->pagination->initialize($config);	

		
		$data['results'] = $this->barang->getItem($config['per_page'], $this->uri->segment(3));
		
		$this->load->view('barang/content_barang_view', $data);
	}
	
	function insert()
	{

	
		$this->open();
		$data['nomor_ka'] = $this->input->post('nomor_ka');
		$data['nama_ka'] = $this->input->post('nama_ka');
		$data['relasi'] = $this->input->post('relasi');
		
		$jam_kedatangan = $this->input->post('jam_kedatangan');
		$menit_kedatangan = $this->input->post('menit_kedatangan');
		$data['jam_kedatangan'] = $jam_kedatangan .':'. $menit_kedatangan;
		
		$perkiraan_jam_kedatangan = $this->input->post('perkiraan_jam_kedatangan');
		$perkiraan_menit_keberangkatan = $this->input->post('perkiraan_menit_keberangkatan');
		$data['perkiraan_jam_datang'] = $perkiraan_jam_kedatangan .':'. $perkiraan_menit_keberangkatan;
		
		$data['status'] = $this->input->post('id_status');
		
		$this->form_validation->set_rules('nomor_ka', 'nomor_ka', 'required');
		/*
		$this->form_validation->set_rules('id_jenis', 'id_jenis', 'required');
		$this->form_validation->set_rules('id_kategori', 'id_kategori', 'required');
		$this->form_validation->set_rules('id_satuan', 'id_satuan', 'required');
		$this->form_validation->set_rules('id_golongan', 'id_golongan', 'required');
		$this->form_validation->set_rules('hpp', 'hpp', 'trim');
		$this->form_validation->set_rules('harga_toko', 'harga_toko', 'trim|numeric');*/
		
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		
		$this->form_validation->set_message('required', 'Field %s harus diisi!');
		$this->form_validation->set_message('numeric', 'Field %s harus diisi hanya dengan angka!');
		
		
		if ($this->form_validation->run() == FALSE){
			
			$this->load->view('barang/barang_add',$data);
			
		}else{	
			$this->barang->insert($data);
			
			$this->session->set_flashdata('message', 'Data Barang Berhasil disimpan.');
			redirect('barang');
		}
		
		$this->close();
	}
	
	
	function update($id)
	{
		
		$this->open();
		
		$data['result']= $this->barang->getItemById($id);
		
		$data['nomor'] = $id;
		$data['nomor_ka'] = $data['result']->row()->nomor_ka;
		$data['nama_ka'] = $data['result']->row()->nama_ka;
		$data['relasi'] = $data['result']->row()->relasi;
		
		$var_jam_kedatangan = $data['result']->row()->jam_kedatangan;
		$explode_jam_kedatangan = explode(':',$var_jam_kedatangan);
		$data['var_jam_kedatangan'] = $explode_jam_kedatangan[0];
		$data['var_menit_kedatangan'] = $explode_jam_kedatangan[1];


		$var_perkiraan_jam_kedatangan = $data['result']->row()->perkiraan_jam_datang;
		$explode_perkiraan_jam_kedatangan = explode(':',$var_perkiraan_jam_kedatangan);
		$data['var_perkiraan_jam_kedatangan'] = $explode_perkiraan_jam_kedatangan[0];
		$data['var_perkiraan_menit_kedatangan'] = $explode_perkiraan_jam_kedatangan[1];

		
		$data['status'] = $data['result']->row()->status;
		
		
		$this->load->view('barang/barang_edit', $data);
		
		$this->close();
	}
	
	function process_update()
	{
		
		$this->open();
		
		$data['nomor'] = $this->input->post('nomor');
		$data['nomor_ka'] = $this->input->post('nomor_ka');
		$data['nama_ka'] = $this->input->post('nama_ka');
		$data['relasi'] = $this->input->post('relasi');
		
		$jam_kedatangan = $this->input->post('jam_kedatangan');
		$menit_kedatangan = $this->input->post('menit_kedatangan');
		$data['jam_kedatangan'] = $jam_kedatangan .':'. $menit_kedatangan;
		
		$perkiraan_jam_kedatangan = $this->input->post('perkiraan_jam_kedatangan');
		$perkiraan_menit_keberangkatan = $this->input->post('perkiraan_menit_keberangkatan');
		$data['perkiraan_jam_datang'] = $perkiraan_jam_kedatangan .':'. $perkiraan_menit_keberangkatan;;
		$data['status'] = $this->input->post('id_status');

		
		$this->form_validation->set_rules('nomor_ka', 'nomor_ka', 'required');
		/*$this->form_validation->set_rules('nama_barang', 'nama_barang');
		$this->form_validation->set_rules('id_jenis', 'id_jenis', 'required');
		$this->form_validation->set_rules('id_kategori', 'id_kategori', 'required');
		$this->form_validation->set_rules('id_satuan', 'id_satuan', 'required');
		$this->form_validation->set_rules('id_golongan', 'id_golongan', 'required');
		$this->form_validation->set_rules('hpp', 'hpp', 'trim');*/

		
		
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		
		$this->form_validation->set_message('required', 'Field %s harus diisi!');
		$this->form_validation->set_message('numeric', 'Field %s harus diisi hanya dengan angka!');
		
		
		if ($this->form_validation->run() == FALSE){
			
			$this->load->view('barang/barang_edit',$data);
			
		}else{	
			$this->barang->update($data['nomor'], $data);
			
			$this->session->set_flashdata('message', 'Data Barang Berhasil diupdate.');
			redirect('barang');
		}
		
		$this->close();
		
	}
	
	function delete($id)
	{

		
		$this->barang->delete($id);
		$this->session->set_flashdata('message', 'Data Barang Berhasil dihapus.');
		redirect('barang');
	}
	
}